﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using agent02.Model;

namespace agent02.Controllers
{
    public class AccountController : Controller
    {
        AgentDBEntities db = new AgentDBEntities();
        // There is no GET: /Account/

        //
        // GET: /Account/LogOn
        public ActionResult LogOn()
        {
            return View();
        }

        [HttpPost]
        public ActionResult LogOn(TAI_KHOAN tk)
        {
            AuthorizeAttribute auth = new AuthorizeAttribute();
            if (ModelState.IsValid)
            {
                var acc = from a in db.TAI_KHOAN
                          where (a.TrangThai == 1) && (a.Username.Equals(tk.Username)) && (a.Password.Equals(tk.Password))
                          select a;
                if (acc.Count() == 1)
                {
                    TAI_KHOAN a = acc.Single();
                    auth.Users = a.Username;
                    FormsAuthentication.SetAuthCookie(auth.Users, false);
                    var role = (from r in db.TAIKHOAN_NHOM
                               where r.TaiKhoan == a.MaTaiKhoan
                               select r).First();
                    // NHAN_VIEN or KHACH_HANG?
                    auth.Roles = role.NHOM1.TenNhom;
                    if (role.NHOM1.TenNhom == "admin")
                    {
                        return RedirectToAction("Index", "Admin");
                    }
                    else if (role.NHOM1.TenNhom == "customer")
                    {
                        return RedirectToAction("Index", "Home");
                    }
                }
                else
                {
                    ModelState.AddModelError("", "Invalid username or password.");
                }
            }
            // something wrong
            return View(tk);
        }

        //
        // GET: /Account/LogOff
        public ActionResult LogOff()
        {
            FormsAuthentication.SignOut();
            return RedirectToAction("Index", "Home");
        }

        //
        // GET: /Account/ChangePassword
        public ActionResult ChangePassword()
        {
            return View();
        }

        [HttpPost]
        public ActionResult ChangePassword(ChangePasswordModel c)
        {
            string username = User.Identity.Name;
            var acc = from a in db.TAI_KHOAN
                      where (a.TrangThai == 1) && (a.Username.Equals(username)) && (a.Password.Equals(c.oldPassword))
                      select a;
            if (acc.Count() == 1)
            {
                if (c.newPassword != c.confirmPassword)
                {
                    ModelState.AddModelError("", "Passwords do not match!");
                    return View(c);
                }
                else
                {
                    acc.Single().Password = c.newPassword;
                    db.SaveChanges();
                    return RedirectToAction("PasswordChanged");
                }
            }
            else
            {
                ModelState.AddModelError("", "Invalid password.");
                return View(c);
            }
        }

        //
        // GET: /Account/PasswordChanged
        public ActionResult PasswordChanged()
        {
            return View();
        }

        //
        // GET: /Account/Register
        public ActionResult Register()
        {
            RegisterModel r = new RegisterModel();
            return View(r);
        }

        [HttpPost]
        public ActionResult Register(RegisterModel r)
        {
            if ((r.khachHang.HoTen == null) || (r.khachHang.CMND == null) || (r.khachHang.Email == null) || (r.taiKhoan.Username == null) || (r.taiKhoan.Password == null) || (r.password == null))
            {
                ModelState.AddModelError("", "Không được bỏ trống dòng có dấu *");
                return View(r);
            }
            else if (r.password != r.taiKhoan.Password)
            {
                ModelState.AddModelError("", "Password không khớp");
                return View(r);
            }
            else
            {
                KHACH_HANG kh = r.khachHang;
                TAI_KHOAN tk = r.taiKhoan;
                TAIKHOAN_NHOM tkn = new TAIKHOAN_NHOM();
                tkn.TaiKhoan = tk.MaTaiKhoan;
                tkn.Nhom = 2; // customer

                db.TAI_KHOAN.AddObject(tk);
                db.KHACH_HANG.AddObject(kh);
                db.TAIKHOAN_NHOM.AddObject(tkn);
                db.SaveChanges();
                return RedirectToAction("Registered");
            }
        }

        //
        // GET: /Account/Registered
        public ActionResult Registered()
        {
            return View();
        }

        //
        // GET: /Account/Profile
        public ActionResult Profile()
        {
            string username = User.Identity.Name;
            var acc = from a in db.TAI_KHOAN
                      where (a.TrangThai == 1) && (a.Username.Equals(username))
                      select a;
            if (acc.Count() == 1)
            {
                if (acc.Single().KHACH_HANG.Count() == 1)
                {
                    ProfileModel p = new ProfileModel();
                    p.taiKhoan = acc.Single();
                    p.khachHang = acc.Single().KHACH_HANG.Single();
                    return View(p);
                }
                else if (acc.Single().NHAN_VIEN.Count() == 1)
                {
                    return RedirectToAction("ChangePassword");
                }
                else
                {
                    return RedirectToAction("Index", "Home");
                }
            }
            else
            {
                return RedirectToAction("Index", "Home");
            }
        }

        //
        // GET: /Account/Edit
        public ActionResult Edit()
        {
            string username = User.Identity.Name;
            var acc = from a in db.TAI_KHOAN
                      where (a.TrangThai == 1) && (a.Username.Equals(username))
                      select a;
            if (acc.Count() == 1)
            {
                if (acc.Single().KHACH_HANG.Count() == 1)
                {
                    ProfileModel p = new ProfileModel();
                    p.taiKhoan = acc.Single();
                    p.khachHang = acc.Single().KHACH_HANG.Single();
                    return View(p);
                }
                else
                {
                    return RedirectToAction("Index", "Home");
                }
            }
            else
            {
                return RedirectToAction("Index", "Home");
            }
        }

        [HttpPost]
        public ActionResult Edit(ProfileModel p)
        {
            if ((p.khachHang.HoTen == null) || (p.khachHang.CMND == null) || (p.khachHang.Email == null))
            {
                ModelState.AddModelError("", "Không được bỏ trống dòng \"Họ Tên\", \"CMND\" và \"Email\"");
                return View(p);
            }
            else
            {
                var acc = from a in db.TAI_KHOAN
                          where a.Username == User.Identity.Name
                          select a;
                if (acc.Count() != 1)
                {
                    return RedirectToAction("Index", "Home");
                }

                string maTaiKhoan = acc.Single().MaTaiKhoan;

                var customer = from c in db.KHACH_HANG
                          where c.MaTaiKhoan == maTaiKhoan
                          select c;
                if (customer.Count() != 1)
                {
                    return RedirectToAction("Index", "Home");
                }

                var cus = customer.Single();

                if (p.khachHang.HoTen != null)
                {
                    cus.HoTen = p.khachHang.HoTen;
                }
                if (p.khachHang.CMND != null)
                {
                    cus.CMND = p.khachHang.CMND;
                }
                if (p.khachHang.DiaChi != null)
                {
                    cus.DiaChi = p.khachHang.DiaChi;
                }
                if (p.khachHang.Email != null)
                {
                    cus.Email = p.khachHang.Email;
                }
                if (p.khachHang.SDT != null)
                {
                    cus.SDT = p.khachHang.SDT;
                }
                if (p.khachHang.TenNganHang != null)
                {
                    cus.TenNganHang = p.khachHang.TenNganHang;
                }
                if (p.khachHang.TaiKhoanNganHang != null)
                {
                    cus.TaiKhoanNganHang = p.khachHang.TaiKhoanNganHang;
                }
                db.SaveChanges();
                return RedirectToAction("Profile");
            }
        }
    }
}
